import pygame
from settings import Settings
from ship import Ship
import game_function as gf
from pygame.sprite import Group
from game_stats import GameStats
from button import Button
from scoreboard import Scoreboard
from alien import Alien


def run_game():
    # 初始化pygame并且创建一个screen对象
    pygame.init()
    ai_settings = Settings()
    screen = pygame.display.set_mode(
        (ai_settings.screen_width, ai_settings.screen_lenth)
    )
    pygame.display.set_caption('Alien Invasion')

    # 初始化ship bullets alien stats button sb
    ship = Ship(ai_settings, screen)
    bullets = Group()
    aliens = Group()
    stats = GameStats(ai_settings)
    sb = Scoreboard(ai_settings, screen, stats)
    play_button = Button(ai_settings, screen, "Play")

    # 初始化aliens
    gf.create_fleet(ai_settings, screen, ship, aliens)

    # 做一个循环使得只有在用户想要结束时结束程序
    while True:
        # 监听是否用户点击结束按钮
        gf.check_events(ai_settings, screen, stats, sb, play_button, ship, aliens, bullets)
        if stats.game_active:
            ship.update()
            gf.update_bullets(ai_settings, screen, stats, sb, ship, aliens, bullets)
            gf.update_aliens(ai_settings, stats, sb, screen, ship, aliens, bullets)

        gf.update_screen(ai_settings, screen, stats, sb, ship, bullets, aliens,
                         play_button)

run_game()
